package org.qs.webservice.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.qs.webservice.exception.QSReadException;


/**
 * 时间操作类
 * @ClassName: DateUtil 
 * @author xusaike 
 * @date 2015年10月13日 上午9:14:18 
 *
 */
public final class DateUtil {
	/**
	 * 日期转换成字符串
	 * @Title: getString 
	 * @param date
	 * @param unq   yyyy-MM-dd HH:mm:ss
	 * @return   
	 * @return String    
	 * @throws
	 */
	public static String getString(Date date, String unq){
		if(date==null){
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(unq);
		return format.format(date);
	}
    public static String getDistanceTime(Long diff) { 
       
		long day = 0; 
		long hour = 0; 
		long min = 0; 
		long sec = 0; 
        day = diff / (24 * 60 * 60 * 1000); 
        hour = (diff / (60 * 60 * 1000) - day * 24); 
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); 
        sec = (diff/1000-day*24*60*60-hour*60*60-min*60); 
        
        return  hour + "时" + min + "分" + sec + "秒"; 
    } 
    public static String getDistanceDate(Long diff) { 
        
		long day = 0; 
		long hour = 0; 
		long min = 0; 
	//	long sec = 0; 
        day = diff / (24 * 60 * 60 * 1000); 
        hour = (diff / (60 * 60 * 1000) - day * 24); 
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); 
      //  sec = (diff/1000-day*24*60*60-hour*60*60-min*60); 
        
        return  day+"天"+hour + "时" + min + "分"; 
    } 
    public static String getDistanceDay(Long diff) { 
        
		long day = 0; 
		long hour = 0; 
		long min = 0; 
		/*long sec = 0; */
        day = diff / (24 * 60 * 60 * 1000); 
        hour = (diff / (60 * 60 * 1000) - day * 24); 
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); 
       /* sec = (diff/1000-day*24*60*60-hour*60*60-min*60); */
        if(hour>=8L){
        	hour=8L;
        }
        double minD =min/60.0+hour;
        
        return  minD+""; 
    } 
	/**
	 * 字符串转换成日期
	 * @Title: StrToDate 
	 * @param str
	 * @param unq
	 * @return   
	 * @return Date    
	 * @throws
	 */
	public static Date StrToDate(String str, String unq) {
		SimpleDateFormat format = new SimpleDateFormat(unq);
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("str", str);
			errorJson.put("unq", unq);
			QSReadException.printf(e, "类型转换异常！",errorJson);
		}
		return date;
	}
	

	
}
